package com.yd.app.service.pc.permissionService;

import com.yd.app.mapper.PermissionMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.Perm;
import com.yd.domain.searchParam.PermParam;
import com.yd.domain.sys.Role;
import com.yd.domain.sys.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-07 11:13
 * @desc权限的service
 **/
@Service
public class PermissionService extends BaseService {
    @Autowired
    private PermissionMapper permissionMapper;
    /**
     * @Author: zy
     * @Desc:新增权限
     * @Date:  2018/3/7
     */
    public void addPerm(Perm p){
        permissionMapper.addPer(p);
    }
    /**
     * @Author: zy
     * @Desc:根据code删除权限
     * @Date:  2018/3/7
     */
    public void deletePermByCode(String code){
        permissionMapper.deletePerByCode(code);
    }
    /**
     * @Author: zy
     * @Desc:修改权限
     * @Date:  2018/3/7
     */
    public void updatePerm(Perm p){
        permissionMapper.updatePer(p);
    }
    /**
     * @Author: zy
     * @Desc:根据code查询权限
     * @Date:  2018/3/7
     */
    public Perm getPermByID(String code){
        Perm p=new Perm();
        p=permissionMapper.getPermByID(code);
        return p;
    }
    /**
     * @Author: zy
     * @Desc:分页检索权限数据
     * @Date:  2018/3/7
     */
    public PageModel getPermList(PermParam p, int pageIndex, int pageSize){
        PageModel pm=new PageModel();
        List<Perm> list=new ArrayList<>();
        list=permissionMapper.searchPermList(p,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=permissionMapper.searchPermCount(p);
            pm.setCount(count);
        }
        pm.setList(list);
        return pm;
    }
    /**
     * 根据角色获取权限列表
     * @param roleCode
     * @return
     */
    public List<Perm> getPermListByRoleCode(String roleCode) {
        List<Perm> list=permissionMapper.getPermListByRoleCode(roleCode);
        return list;
    }

    /**
     * 获取所有权限
     * @return
     */
    public List<Perm> getAllPermList() {
        List<Perm> list=permissionMapper.getAllPermList();
        return list;
    }
    /**
     * 向角色添加权限
     * @return
     */
    public void addPermToRole(String roleCode, String permId) {
        permissionMapper.addPermToRole(roleCode,permId);
    }

    /**
     * 删除角色权限
     * @param roleCode
     * @param permId
     */
    public void deletePermToRole(String roleCode, String permId) {
        permissionMapper.deletePermToRole(roleCode,permId);
    }
    //获取用户角色
    public List<Role> getUserRoleList(String userId) {
        List<Role>list= permissionMapper.getUserRoleList(userId);
        return list;
    }

    /**
     * 根据权限获取用户
     * @param permCode
     * @return
     */
    public List<User>getUserByPermCode(String permCode){
        List<User>users=  permissionMapper.getUserByPermCode(permCode);
        return  users;
    }
}
